from flask_restful import Resource, request

from database import Database


# 创建数据库连接


# Mock user database
class getUsers(Resource):

    def get(self):
        data = request.get_json()
        username = data.get('username')
        db = Database()

        users = db.execute_query("SELECT * FROM useraccount ")
        print(users)
        all_users = []
        if users:
            users_formatted = []
            for user in users:
                user_dict = {
                    "userId": user[0],
                    "type": user[1],
                    "password": user[2],
                    "firstName": user[4],
                    "lastName": user[5],
                    "email": user[6],
                    "address": user[7],
                    "imageUrl": user[8]
                }
                users_formatted.append(user_dict)
            if username == 'manager':
                managers = db.execute_query("SELECT * FROM manageraccount ")
                managers_formatted = []
                for manager in managers:
                    manager_dict = {
                        "managerId": manager[0],
                        "password": manager[1],
                        "firstName": manager[2],
                        "lastName": manager[3],
                        "imageUrl": user[4],
                        "email": manager[5]
                    }
                    managers_formatted.append(manager_dict)
            all_users.append(users_formatted)
            all_users.append(managers_formatted)
            return {"users": all_users}, 200
        else:
            return {"message": "server internal error"}, 401
